#include "mutant.h"
#include <math.h>
#include <iostream>
#define PI 3.14159265

#pragma region base
mutant::mutant() {
}


mutant::~mutant() {
}

#pragma endregion

	
#pragma region publics


	void mutant::Initialize(sf::RenderWindow* window, Player* player, sf::Texture* walkImage, sf::Texture* attackImage) {
		m_renderWindow = window;
		m_player = player;

		m_walkImage = walkImage;

		playerDown = false;

		m_attackAnimation = new Animation();
		m_attackAnimation->Initialize(attackImage, sf::Vector2f(110, 124), 17, 17);

		m_walkAnimation = new Animation();
		m_walkAnimation->Initialize(m_walkImage, sf::Vector2f(80, 90), 10, 12);

		m_sprite = new sf::Sprite();
		m_attackSprite = new sf::Sprite();
		m_sprite->setTexture(*m_walkImage);

		m_attackSprite->setTexture(*attackImage);

		m_isBusy = false;
		clockRestarted = false;

		m_clock = new sf::Clock();
		m_chaseClock = new sf::Clock();
		//m_sprite->setOrigin(m_image->getSize().x / 2, m_image->getSize().y / 2);
		

		m_health = 6;

		speed = 40.0f;
		
		m_attackClock = new sf::Clock();
		
		hasStartedChasing = false;
		
		
		/*
		if(m_growlBuffer.loadFromFile("sounds/CrazyHumanHighPitch1.wav"))
		{
			 std::cout<<"load sound"<<std::endl;
		}*/

	}
	void mutant::Update() {
		
		if (m_growl.getStatus()==sf::SoundSource::Stopped)
	{
		//std::cout << "growl" << std::endl;
		m_growl.play();
	}
		timeSinceLastFrame = m_clock->restart();
		playerAngle = atan2(((m_player->m_playerSprite->getPosition().y) - m_sprite->getPosition().y), ((m_player->m_playerSprite->getPosition().x) - m_sprite->getPosition().x));
		shipAngle = atan2((720 - (m_sprite->getPosition().y)), (1280 - (m_sprite->getPosition().x)));

		//std::cout << playerAngle << std::endl;
		// Distance between mutants and (ship)
		if (m_sprite->getPosition().x < 1280) {
			distanceXship = 1280 - m_sprite->getPosition().x;
		} else if (m_sprite->getPosition().x > 1280) {
			distanceXship = m_sprite->getPosition().x - 1280;
		}
		if (m_sprite->getPosition().y < 720) {
			distanceYship = 720 - m_sprite->getPosition().y;
		} else if (m_sprite->getPosition().y > 720) {
			distanceYship = m_sprite->getPosition().y -720;
		}
		// Distance between mutants and player
		if(m_sprite->getPosition().x < m_player->m_playerSprite->getPosition().x) {
			distanceXplayer = m_player->m_playerSprite->getPosition().x - m_sprite->getPosition().x;
		} else if (m_sprite->getPosition().x > m_player->m_playerSprite->getPosition().x) {
			distanceXplayer = m_sprite->getPosition().x - m_player->m_playerSprite->getPosition().x;
		}

		if(m_sprite->getPosition().y < m_player->m_playerSprite->getPosition().y) {
			distanceYplayer = m_player->m_playerSprite->getPosition().y - m_sprite->getPosition().y;
		} else if (m_sprite->getPosition().y > m_player->m_playerSprite->getPosition().y) {
			distanceYplayer = m_sprite->getPosition().y - m_player->m_playerSprite->getPosition().y;
		}

		
		
		// Calculate distance to player and ship
		distancePlayer = sqrt(distanceXplayer*distanceXplayer + distanceYplayer*distanceYplayer);
		distanceShip = sqrt(distanceXship*distanceXship + distanceYplayer*distanceYplayer);

		if (playerDown == false) {
			if(distancePlayer <= distanceShip && chaseTime.asSeconds() < 5.0f) {

				if(distancePlayer <= 200)
				{
					if(hasStartedChasing == false)
					{
						m_chaseClock->restart();
						hasStartedChasing = true;
					}
				}
				if(hasStartedChasing == true)
				{
					chaseTime += m_chaseClock->restart();
				}

		
				//m_sprite->setRotation(playerAngle * 180 / PI * 1 + 180);
				m_sprite->setRotation(playerAngle * 180 / PI);

				m_sprite->move(speed * cos(playerAngle) * timeSinceLastFrame.asSeconds(),
				speed * sin(playerAngle) * timeSinceLastFrame.asSeconds());
			}
		}


		if(distancePlayer > distanceShip || chaseTime.asSeconds() > 5.0f || playerDown == true) {
			//m_sprite->setRotation(shipAngle * 180 / PI * 1 + 180);
			m_sprite->setRotation(shipAngle * 180 / PI);
			clockRestarted = false;
			m_sprite->move(speed * cos(shipAngle) * timeSinceLastFrame.asSeconds(),
			speed * sin(shipAngle) * timeSinceLastFrame.asSeconds());
		}

		m_walkAnimation->GetSprite()->setPosition(m_sprite->getPosition());
		m_attackAnimation->GetSprite()->setPosition(m_sprite->getPosition());
		m_attackAnimation->GetSprite()->setRotation(m_sprite->getRotation());

		if(speed == 40.0f)
		{
			m_sprite = m_walkAnimation->GetSprite();
			m_sprite->setOrigin(40, 45);
			m_walkAnimation->Update(timeSinceLastFrame.asSeconds());
		}
		if (speed == 0) {
			m_sprite = m_attackAnimation->GetSprite();
			m_sprite->setOrigin(55, 45);
			m_attackAnimation->Update(timeSinceLastFrame.asSeconds());
		}
		
		speed = 40.0f;
		

		m_renderWindow->draw(*m_sprite);
	}

	void mutant::attack_player(sf::Sound* mutantAttack)
	{
		if (playerDown == true) {
			speed = 40;
		} else {
			speed = 0;
		}
			m_attackTimer += m_attackClock->restart();
		

		if(m_attackTimer.asSeconds() > 1.0f)
		{
			m_player->m_playerHealth -= 10.0f;
			m_attackTimer -= m_attackTimer;
			mutantAttack->play();
		}
		
		
	}

	void mutant::attack_ship(sf::Sound* mutantAttack)
	{
			speed = 0;
			
			m_attackTimer += m_attackClock->restart();
		

		if(m_attackTimer.asSeconds() > 1.0f)
		{
			
			m_player->shipHealth -= 100;
			m_attackTimer -= m_attackTimer;
			//mutantAttack->play();
		}
	}

	void mutant::Cleanup() {

	//delete m_image;
	delete m_sprite;
	delete m_attackSprite;
	delete m_attackAnimation;
	delete m_walkAnimation;
	delete m_clock;
	delete m_attackClock;
	delete m_chaseClock;

	
	}

	void mutant::Spawn(float xPos, float yPos) {
		m_sprite->setPosition(xPos, yPos);
		m_isBusy = true;
	}

	bool mutant::IsBusy() { 
		return m_isBusy;
	}

	void mutant::Destroy() {
		m_isBusy = false; 
	}


	sf::FloatRect mutant::GetRect() { 
		return sf::FloatRect(m_sprite->getPosition().x, m_sprite->getPosition().y,
			m_sprite->getPosition().x + m_sprite->getGlobalBounds().width, m_sprite->getPosition().y + m_sprite->getGlobalBounds().height);
	}


#pragma endregion



#pragma region privates





#pragma endregion